package org.springblade.application.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.application.command.DashboardAndDisplaysCommand;
import org.springblade.application.command.TargetAddressPageCommand;
import org.springblade.application.service.ITargetAddressService;
import org.springblade.application.util.ConfigInfoUtil;
import org.springblade.applicationapi.entity.TargetAddressEntity;
import org.springblade.applicationapi.vo.TargetAddressVO;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author wzy
 * @date 2022/1/6
 */
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
@RestController
@RequestMapping("application/target_address")
@Api(value = "应用中心",tags = "应用中心")
public class TargetAddressController extends BladeController {

	@Resource
	ITargetAddressService service;
	@Resource
	private ConfigInfoUtil configInfoUtil;
	/**
	 * 分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页", notes = "分页")
	public R<IPage<TargetAddressVO>> page(@ApiParam(value = "应用中心") TargetAddressPageCommand pageCommand, Query query) {
		IPage<TargetAddressVO> addressPage =
			service.addressPage(pageCommand, Condition.getPage(query));
		return R.data(addressPage);
	}
	/**
	 * 无限制分页
	 */
	@GetMapping("/unlimited_page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "无限制分页", notes = "无限制分页")
	public R<IPage<TargetAddressVO>> unlimitedPage(@ApiParam(value = "应用中心") TargetAddressPageCommand pageCommand, Query query) {
		IPage<TargetAddressVO> unlimitedPage =
			service.unlimitedPage(pageCommand, Condition.getPage(query));
		return R.data(unlimitedPage);
	}

	/**
	 * 新增
	 */
	@PostMapping("/add")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增应用中心", notes = "新增应用中心")
	public R<Object> add(@ApiParam(value = "应用中心") @RequestBody TargetAddressEntity entity) throws Exception {
		return R.data(service.add(entity));
	}

	/**
	 * 修改
	 */
	@PostMapping("/edit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改", notes = "修改")
	public R<Object> edit(@ApiParam(value = "应用中心") @RequestBody TargetAddressEntity entity)  {
		return R.status(service.edit(entity));
	}
	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "删除", notes = "删除")
	public R<Object> remove(@ApiParam(value = "应用中心表ids")  @RequestParam(value = "ids",required = false) List<Long> ids) {
		return R.status(service.removeByid(ids));
	}

	/**
	 * 新增配置信息
	 */
	@PostMapping("/config_info")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增配置信息", notes = "新增配置信息")
	public R<Object> configInfo(@ApiParam(value = "应用中心表id") @RequestParam  Long id
		,@ApiParam(value = "配置信息") @RequestParam  String configInfo){
		return R.data(service.configInfo(id,configInfo));
	}
	/**
	 * 查询配置信息
	 */
	@PostMapping("/get_config_info")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "查询配置信息", notes = "查询配置信息")
	public R<JSONObject> configInfo(@ApiParam(value = "应用中心表id") @RequestParam  Long id){
		String configInfo = service.getById(id).getConfigInfo();
		JSONObject jsonObject = JSONObject.parseObject(configInfo);
		Map<String, Object> info = configInfoUtil.getInfo();
		jsonObject.put("bigscreenAddress",info.get("address"));
		return R.data(jsonObject);
	}






}
